package site.shihuan.helpdesk.auth.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.shihuan.helpdesk.auth.dao.AuthDeptDao;
import site.shihuan.helpdesk.auth.model.po.AuthDeptPO;
import site.shihuan.helpdesk.auth.model.vo.DeptVO;
import site.shihuan.helpdesk.common.util.IdWorker;

import java.util.Date;
import java.util.List;

/**
 * 部门业务层
 *
 * @author 周世焕
 * @Date 2021/3/24 20:05
 */
@Service
@Slf4j
public class DeptService {

    @Autowired
    private AuthDeptDao authDeptDao;

    /**
     * 查询部门列表
     * @param tenantId
     * @return
     */
    public List<DeptVO> findByTenant(String tenantId) {
        return authDeptDao.selectByTenant(tenantId);
    }

    public AuthDeptPO createDept(String tenantId,String name) {
        AuthDeptPO authDeptPO = new AuthDeptPO();
        authDeptPO.setName(name);
        authDeptPO.setTenantId(tenantId);
        authDeptPO.setDeptId(IdWorker.nextId());
        authDeptPO.setGmtCreate(new Date(System.currentTimeMillis()));
        authDeptPO.setGmtModified(new Date(System.currentTimeMillis()));
        authDeptDao.insert(authDeptPO);
        return authDeptPO;
    }

    public void updateDept(String tenantId,String deptId,String name) {
        AuthDeptPO authDeptPO = new AuthDeptPO();
        authDeptPO.setName(name);
        authDeptPO.setTenantId(tenantId);
        authDeptPO.setDeptId(deptId);
        authDeptDao.updateByTenantAndDept(authDeptPO);
    }

    public void deleteDeptBatch(String tenantId,String[] deptIds) {
        authDeptDao.deleteByTenantAndDeptBatch(tenantId,deptIds);
    }
}
